<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SysUser extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $model->create_by = $model->user_name;
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onbeforeUpdate($model)
    {
        $model->update_by = $model->user_name;
    }

    public function getUserByUsername($username){
        if(empty($username)){
            return false;
        }
        $where = [
            "user_name" =>$username,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }

        $where = [
            "user_id"=>$id,
        ];
        return $this->where($where)->find()->save($data);
    }

    /**
     * 查询用户列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalUserList($where, $field,$limit,$page){
        $order = [
            "user_id" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据$id获取用户数据
     * @param $id
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByIdFind($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "user_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function getUserByNameIdFind($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['user_id','in',$ids],
            ['user_name','=',"admin"]
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 更改用户状态
     * @param $userId
     * @param $status
     * @return bool
     */
    public function changeStatusUserModel($userId,$status){
        if(empty($userId)){
            return false;
        }
        $where = [
            ['user_id', '=', $userId]
        ];
        $data = [
            'status'=>$status
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    /**
     * 更改用户密码
     * @param $userId
     * @param $status
     * @return bool
     */
    public function resetPwdUserModel($userId,$password){
        if(empty($userId)){
            return false;
        }
        $where = [
            ['user_id', '=', $userId]
        ];
        $data = [
            'password'=>md5(config("redis.password_salt").$password)
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    /**
     * 根据用户名称获取用户数据
     * @param $name
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByNameFind($user_name,$user_id){
        if(empty($user_name)){
            return false;
        }
        $where = [
            "user_name" =>$user_name,
            "del_flag" => "0",
        ];
        if($user_id){
            $result = $this->where("user_id", "<>", $user_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    public function updateUserById($id,$data){
        if(empty($id) || empty($data)){
            return false;
        }
        $where = [
            "user_id" =>$id,
        ];
        $result = $this->where($where)->find()->save($data);
        return $result;
    }

    public function DelUserIdsData($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['user_id', 'in', $ids]
        ];
        $data = [
            'del_flag'=>'2'
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function getUserByIdArray($userId){
        if(empty($userId)){
            return false;
        }
        $where = [
            "user_id" =>$userId,
        ];
        $result = $this->where($where)->select();
        return $result ? $result->toArray() : [];
    }
}
